package cn.fl.seccar.service.impl;

import java.util.ArrayList;
import java.util.List;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import fl.seccar.facade.auccar.condition.AucCarInfoQC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.seccar.service.IAucCarInfoService;
import cn.fl.seccar.transform.AucCarInfoVOTransform;
import cn.fl.seccar.vo.AucCarInfoVO;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.transform.SysUlFilesRecVOTransform;
import cn.fl.system.vo.SysUlFilesRecVO;
import fl.seccar.facade.auccar.api.IAucCarAddApiService;
import fl.seccar.facade.auccar.api.IAucCarModifyApiService;
import fl.seccar.facade.auccar.api.IAucCarQueryApiService;
import fl.seccar.facade.auccar.constant.AucCarInfoConstant;
import fl.seccar.facade.auccar.dto.AucCarInfoDTO;

/**
 * @Description: 竞拍车辆信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AucCarInfoServiceImpl implements IAucCarInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IAucCarInfoService.class);

	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IAucCarQueryApiService aucCarQueryApiService;

	@Autowired
	private IAucCarAddApiService aucCarAddApiService;

	@Autowired
	private IAucCarModifyApiService aucCarModifyApiService;

	/**
	 * 本地数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private IUploadApiService uploadApiService;

	@Autowired
	private ISysUlFilesRecService sysUlFilesRecService;

	@Override
	public AucCarInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		AucCarInfoDTO dto = this.aucCarQueryApiService.getAucCarInfoDTO(id);
		return AucCarInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(AucCarInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		Long id = this.aucCarAddApiService.addAucCarInfo(AucCarInfoVOTransform.toDTO(entity));
		if(CollectionUtils.isNotEmpty(entity.getUploadFilesRecVOs())){
			//删除之前的附件信息
			SysUlFilesRecQueryCondition	fileQc = new SysUlFilesRecQueryCondition();
			fileQc.setSysCd(CurrentThreadContext.getCurrentSysCd());
			fileQc.setPkId(String.valueOf(id));
			fileQc.setTabNm("AUC_CAR_INFO");
			fileQc.setOrgCd(PrjBscInfoConstant.CODE_PRJ_FILE_DEF_ORG);
			fileQc.setSysCd(CurrentThreadContext.getCurrentSysCd());
			fileQc.setCfgCd(AucCarInfoConstant.AUC_CAR_INFO_FILE_CODE);
			fileQc.setCfgDtlCd(AucCarInfoConstant.AUC_CAR_INFO_FILE_DETAIL_CODE);
			List<SysUlFilesRecVO> fileList = SysUlFilesRecVOTransform.toVOList(this.uploadApiService.searchSysUlFilesRecList(fileQc));
			List<Long> idList = new ArrayList<Long>();
			if(CollectionUtils.isNotEmpty(fileList)){
				for (SysUlFilesRecVO sysUlFilesRecVO : fileList) {
					idList.add(sysUlFilesRecVO.getId());
				}
				this.uploadApiService.removeSysUlFilesRecByIds(idList);
			}
			//附件信息维护
			List<SysUlFilesRecVO>  sysUlFilesRecVOs = entity.getUploadFilesRecVOs();
			for(SysUlFilesRecVO sysUlFilesRecVO:sysUlFilesRecVOs){
			sysUlFilesRecVO.setPkId(String.valueOf(id));
			sysUlFilesRecVO.setTabNm("AUC_CAR_INFO");
			sysUlFilesRecVO.setIsDel(BaseConstant.IS_YESNO_NO);
			sysUlFilesRecVO.setOrgCd(PrjBscInfoConstant.CODE_PRJ_FILE_DEF_ORG);
			sysUlFilesRecVO.setSysCd(CurrentThreadContext.getCurrentSysCd());
			sysUlFilesRecVO.setCfgCd(AucCarInfoConstant.AUC_CAR_INFO_FILE_CODE);
			sysUlFilesRecVO.setCfgDtlCd(AucCarInfoConstant.AUC_CAR_INFO_FILE_DETAIL_CODE);
			this.sysUlFilesRecService.add(sysUlFilesRecVO);
			}
		}
		return id;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.aucCarInfoApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}

	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AucCarInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		boolean flag = this.aucCarModifyApiService.modifyAucCarInfo(AucCarInfoVOTransform.toDTO(entity));
		if(CollectionUtils.isNotEmpty(entity.getUploadFilesRecVOs())){
			//删除之前的附件信息
			SysUlFilesRecQueryCondition	fileQc = new SysUlFilesRecQueryCondition();
			fileQc.setSysCd(CurrentThreadContext.getCurrentSysCd());
			fileQc.setPkId(String.valueOf(entity.getId()));
			fileQc.setTabNm("AUC_CAR_INFO");
			fileQc.setOrgCd(PrjBscInfoConstant.CODE_PRJ_FILE_DEF_ORG);
			fileQc.setSysCd(CurrentThreadContext.getCurrentSysCd());
			fileQc.setCfgCd(AucCarInfoConstant.AUC_CAR_INFO_FILE_CODE);
			fileQc.setCfgDtlCd(AucCarInfoConstant.AUC_CAR_INFO_FILE_DETAIL_CODE);
			List<SysUlFilesRecVO> fileList = SysUlFilesRecVOTransform.toVOList(this.uploadApiService.searchSysUlFilesRecList(fileQc));
			List<Long> idList = new ArrayList<Long>();
			if(CollectionUtils.isNotEmpty(fileList)){
				for (SysUlFilesRecVO sysUlFilesRecVO : fileList) {
					idList.add(sysUlFilesRecVO.getId());
				}
				this.uploadApiService.removeSysUlFilesRecByIds(idList);
			}
			//附件信息维护
			List<SysUlFilesRecVO>  sysUlFilesRecVOs = entity.getUploadFilesRecVOs();
			for(SysUlFilesRecVO sysUlFilesRecVO:sysUlFilesRecVOs){
			sysUlFilesRecVO.setPkId(String.valueOf(entity.getId()));
			sysUlFilesRecVO.setTabNm("AUC_CAR_INFO");
			sysUlFilesRecVO.setIsDel(BaseConstant.IS_YESNO_NO);
			sysUlFilesRecVO.setOrgCd(PrjBscInfoConstant.CODE_PRJ_FILE_DEF_ORG);
			sysUlFilesRecVO.setSysCd(CurrentThreadContext.getCurrentSysCd());
			sysUlFilesRecVO.setCfgCd(AucCarInfoConstant.AUC_CAR_INFO_FILE_CODE);
			sysUlFilesRecVO.setCfgDtlCd(AucCarInfoConstant.AUC_CAR_INFO_FILE_DETAIL_CODE);
			this.sysUlFilesRecService.add(sysUlFilesRecVO);
			}
		}
		return flag;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.aucCarQueryApiService.searchAucCarInfoPage(page);
		List<AucCarInfoVO> aucCarInfoVOs= AucCarInfoVOTransform.toVOList((List<AucCarInfoDTO>)page.getResultList());
		page.setResultList(aucCarInfoVOs);
		return page;
	}

	@Override
	public List<KeyValueVO> searchYNList() throws BaseException {
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_SYS_YES_NO_CD, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public DataResultVO exportCarInfoList(AucCarInfoQC condition) throws BaseException{
		if(null == condition){
			condition = new AucCarInfoQC();
		}
		try {
			DataResultDTO resultDTO=this.aucCarQueryApiService.exportCarInfoList(condition);
			if(null==resultDTO){
				return new DataResultVO(BaseConstant.IS_YESNO_NO,"导出失败");
			}else{
				return new DataResultVO(Integer.parseInt(resultDTO.getCode()),resultDTO.getInfo(),resultDTO.getData());
			}
		} catch (BaseException e) {
			BaseLogUtils.error(logger,"exportDecomposeList","批量导出车辆数据失败");
			BaseLogUtils.error(logger, "-", "异常", e);
		} catch (NumberFormatException e) {
			BaseLogUtils.error(logger,"exportDecomposeList","批量导出车辆数据失败");
			BaseLogUtils.error(logger, "-", "异常", e);
		}
		return new DataResultVO(BaseConstant.IS_YESNO_NO,"导出失败");
	}
}
